<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-form label-width="80px" :inline="true" :disabled="disadled">
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" clearable filterable v-model="(category1Id as number)" @change="getCategory2List">
          <el-option :label="category1.name" :value="category1.id" v-for="category1 in category1list" :key="category1.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" clearable filterable v-model="(category2Id as number)" @change="getCategory3List">
          <el-option :label="category2.name" :value="category2.id" v-for="category2 in category2list" :key="category2.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" clearable filterable v-model="(category3Id as number)">
          <el-option :label="category3.name" :value="category3.id" v-for="category3 in category3list" :key="category3.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <!-- card body -->
  </el-card>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Cotegory",
});
</script>

<script setup lang="ts">
import { onMounted } from "vue";
import { ElMessage } from "element-plus";
import { storeToRefs } from "pinia";
import useCategoryStore from "@/stores/category";

const categoryStore = useCategoryStore();
const { getCategory1List, getCategory2List, getCategory3List } = categoryStore;
const {
  category1list,
  category1Id,
  category2list,
  category2Id,
  category3list,
  category3Id,
} = storeToRefs(categoryStore);

defineProps<{
  disadled: boolean;
}>();
/**
 * 初始化获取一级分类
 */
onMounted(() => {
  try {
    getCategory1List();
  } catch (error: any) {
    ElMessage.error("获取一级分类失败:" + error.message);
  }
});
</script>

<style scoped></style>
